package com.mx.zb.mng.utils.context;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class CommonRequestWrapper extends HttpServletRequestWrapper {
	private final String body;

	public CommonRequestWrapper(HttpServletRequest request) throws IOException {
	   super(request);
	   StringBuilder stringBuilder = new StringBuilder();
	   BufferedReader bufferedReader = null;
	   try {
	     InputStream inputStream = request.getInputStream();
	     if (inputStream != null) {
	       bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
	       char[] charBuffer = new char[128];
	       int bytesRead = -1;
	       while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
	         stringBuilder.append(charBuffer, 0, bytesRead);
	       }
	     } else {
	       stringBuilder.append("");
	     }
	   } catch (IOException ex) {
	       throw ex;
	   } finally {
	     if (bufferedReader != null) {
	       try {
	         bufferedReader.close();
	       } catch (IOException ex) {
	         throw ex;
	       }
	     }
	   }
	   body = stringBuilder.toString();
	 }

	@Override
	public ServletInputStream getInputStream() throws IOException {
		return new CustomServletInputStream(body); 
	}

	@Override
	public BufferedReader getReader() throws IOException {
		return new BufferedReader(new InputStreamReader(this.getInputStream()));
	}

	public String getBody() {
		return this.body;
	}
	
	
	private class CustomServletInputStream extends ServletInputStream {  
        private ByteArrayInputStream buffer;  
  
        public CustomServletInputStream(String body) {  
            body = body == null ? "" : body;  
            this.buffer = new ByteArrayInputStream(body.getBytes());  
        }  
  
        @Override  
        public int read() throws IOException {  
            return buffer.read();  
        }  
  
        @Override  
        public boolean isFinished() {  
            return buffer.available() == 0;  
        }  
  
        @Override  
        public boolean isReady() {  
            return true;  
        }  
  
        @Override  
        public void setReadListener(ReadListener listener) {  
            throw new RuntimeException("Not implemented");  
        }  
    } 
}
